package com.ihisun.tmhcms.core.service.orderservice.impl;

import com.ihisun.tmhcms.core.dao.citydao.CityDao;
import com.ihisun.tmhcms.core.dao.mechanismdao.EditAddressDao;
import com.ihisun.tmhcms.core.dao.orderdao.OrderDao;
import com.ihisun.tmhcms.core.dao.userdao.UserDao;
import com.ihisun.tmhcms.core.dto.citydto.CityDto;
import com.ihisun.tmhcms.core.dto.mechanismdto.EditAddressDto;
import com.ihisun.tmhcms.core.dto.orderdto.SearchOrderDto;
import com.ihisun.tmhcms.core.service.orderservice.OrderService;
import com.ihisun.tmhcms.core.vo.OrderVo;
import com.ihisun.tmhcms.core.vo.UserVo;
import com.ihisun.tmhcms.utils.PageBean;
import com.ihisun.tmhcms.utils.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    OrderDao orderDao;
    @PersistenceContext
    EntityManager entityManager;
    @Resource
    EditAddressDao editAddressDao;
    @Resource
    CityDao cityDao;
    @Resource
    UserDao userDao;
    @Override
    public ResponseVo findByOrderStateCountNum() {
        List<Map<String, Integer>> byOrderStateCountNum = orderDao.findByOrderStateCountNum();
        return ResponseVo.ok(byOrderStateCountNum);
    }

    @Override
    public ResponseVo SearchOrder(SearchOrderDto searchOrderDto) {
        StringBuilder sql = new StringBuilder("select * from o_order where 1=1");
        if (searchOrderDto.getOrderState() != null && !"".equals(searchOrderDto.getOrderState())) {
            sql.append(" and order_state =" + searchOrderDto.getOrderState() + "");
        }
        if (searchOrderDto.getNickName() != null && !"".equals(searchOrderDto.getNickName())) {
            sql.append(" and nick_name like concat('%'," + searchOrderDto.getNickName() + ",'%')");
        }
        if (searchOrderDto.getBookingPerson() != null && !"".equals(searchOrderDto.getBookingPerson())) {
            sql.append(" and booking_person like concat('%'," + searchOrderDto.getBookingPerson() + ",'%')");
        }
        if (searchOrderDto.getPhone() != null && !"".equals(searchOrderDto.getPhone())) {
            sql.append(" and phone like concat('%'," + searchOrderDto.getPhone() + ",'%')");
        }
        if (searchOrderDto.getProjectName() != null && !"".equals(searchOrderDto.getProjectName())) {
            sql.append(" and project_name like concat('%'," + searchOrderDto.getProjectName() + ",'%')");
        }
        if (searchOrderDto.getMechanismName() != null && !"".equals(searchOrderDto.getMechanismName())) {
            sql.append(" and mechanism_name like concat('%'," + searchOrderDto.getMechanismName() + ",'%')");
        }
        if (searchOrderDto.getCity() != null && !"".equals(searchOrderDto.getCity())) {
            sql.append(" and city like concat('%'," + searchOrderDto.getCity() + ",'%')");
        }
        if (searchOrderDto.getStart() != "" && searchOrderDto.getEnd() != "") {
            sql.append(" and order_time >= " + searchOrderDto.getStart() + " and order_time <= " + searchOrderDto.getEnd() + "");
        }
        if (searchOrderDto.getStart() != "" && searchOrderDto.getEnd() == "") {
            sql.append(" and order_time >= " + searchOrderDto.getStart() + "");
        }
        if (searchOrderDto.getEnd() != "" && searchOrderDto.getStart() == "") {
            sql.append(" and order_time <= " + searchOrderDto.getEnd() + "");
        }
        sql.append(" limit " + (searchOrderDto.getPageNum() - 1) * 10 + "," + searchOrderDto.getPageSize() + "");
        log.info("sql=" + sql);
        List<OrderVo> resultList = entityManager.createNativeQuery(sql.toString(), OrderVo.class).getResultList();

        for (int i = 0; i < resultList.size(); i++) {
            if (resultList.get(i).getMechanismId() != null && !"".equals(resultList.get(i).getMechanismId())) {
                EditAddressDto byMechanismId = editAddressDao.findByMechanismId(resultList.get(i).getMechanismId());
                resultList.get(i).setMechanismId(byMechanismId.getAddress());
            }
            if (resultList.get(i).getCity() != null && !"".equals(resultList.get(i).getCity())) {
                CityDto byCityCode = cityDao.findByCityCode(resultList.get(i).getCity());
                resultList.get(i).setCity(byCityCode.getName());
            }
            if (resultList.get(i).getUuid()!=null&&!"".equals(resultList.get(i).getUuid())){
                UserVo byUuid = userDao.findByUuid(resultList.get(i).getUuid());
                resultList.get(i).setUserVo(byUuid);
            }
        }


        StringBuilder countSql = new StringBuilder("select count(*) from o_order where 1=1");
        if (searchOrderDto.getOrderState() != null && !"".equals(searchOrderDto.getOrderState())) {
            countSql.append(" and order_state =" + searchOrderDto.getOrderState() + "");
        }
        if (searchOrderDto.getNickName() != null && !"".equals(searchOrderDto.getNickName())) {
            countSql.append(" and nick_name like concat('%'," + searchOrderDto.getNickName() + ",'%')");
        }
        if (searchOrderDto.getBookingPerson() != null && !"".equals(searchOrderDto.getBookingPerson())) {
            countSql.append(" and booking_person like concat('%'," + searchOrderDto.getBookingPerson() + ",'%')");
        }
        if (searchOrderDto.getPhone() != null && !"".equals(searchOrderDto.getPhone())) {
            countSql.append(" and phone like concat('%'," + searchOrderDto.getPhone() + ",'%')");
        }
        if (searchOrderDto.getProjectName() != null && !"".equals(searchOrderDto.getProjectName())) {
            countSql.append(" and project_name like concat('%'," + searchOrderDto.getProjectName() + ",'%')");
        }
        if (searchOrderDto.getMechanismName() != null && !"".equals(searchOrderDto.getMechanismName())) {
            countSql.append(" and mechanism_name like concat('%'," + searchOrderDto.getMechanismName() + ",'%')");
        }
        if (searchOrderDto.getCity() != null && !"".equals(searchOrderDto.getCity())) {
            countSql.append(" and city like concat('%'," + searchOrderDto.getCity() + ",'%')");
        }
        if (searchOrderDto.getStart() != "" && searchOrderDto.getEnd() != "") {
            countSql.append(" and order_time >= " + searchOrderDto.getStart() + " and order_time <= " + searchOrderDto.getEnd() + "");
        }
        if (searchOrderDto.getStart() != "" && searchOrderDto.getEnd() == "") {
            countSql.append(" and order_time >= " + searchOrderDto.getStart() + "");
        }
        if (searchOrderDto.getEnd() != "" && searchOrderDto.getStart() == "") {
            countSql.append(" and order_time <= " + searchOrderDto.getEnd() + "");
        }
        BigInteger singleResult = (BigInteger) entityManager.createNativeQuery(countSql.toString()).getSingleResult();

        PageBean pageBean = new PageBean(searchOrderDto.getPageNum(), singleResult.intValue(), searchOrderDto.getPageSize(), resultList);
        return ResponseVo.ok(pageBean);
    }

    @Override
    public ResponseVo orderInfo(String orderId) {
        OrderVo byOrderId = orderDao.findByOrderId(orderId);
        if (byOrderId.getCity() != null && !"".equals(byOrderId.getCity())) {
            CityDto byCityCode = cityDao.findByCityCode(byOrderId.getCity());
            byOrderId.setCity(byCityCode.getName());
        }
        if (byOrderId.getMechanismId()!=null&&!"".equals(byOrderId.getMechanismId())){
            EditAddressDto byMechanismId = editAddressDao.findByMechanismId(byOrderId.getMechanismId());
            byOrderId.setMechanismAddress(byMechanismId.getAddress());
        }
        if (byOrderId.getUuid()!=null&&!"".equals(byOrderId.getUuid())){
            UserVo byUuid = userDao.findByUuid(byOrderId.getUuid());
            byOrderId.setUserVo(byUuid);
        }
        return ResponseVo.ok(byOrderId);
    }
}
